Ext.namespace("SysMgr");

SysMgr.GroupEdit2=function(parentWin,roleId,parentId){
	this.roleId=roleId;	
	this.parentId=parentId;
	this.parentWin=parentWin;
	this.loader=new SysMgr.CheckLoader({
		url:'../sysmanager/roleUserTree.form?snaptime=' + Date.parse(new Date()),
		baseParams:{roleId:this.roleId},
		listeners:{
				beforeload:this.expandBranch
			}
	});	
	this.txtName = new Ext.form.TextField({
		fieldLabel: '群组名称',
		name:'roleName',
		allowBlank: false,
        blankText: '请输入群组名称!',
		width: 120,
		maxLength:20
	});
	this.form = new Ext.FormPanel({
		region: 'north',
        bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 70,
        labelAlign: 'right',
        items: [this.txtName]
    });
	this.tree = new Ext.tree.TreePanel({
        width: 360,
        height:320,
        region: 'center',
        useArrows:true,
        autoScroll:true,
        animate:true,
        enableDD:true,
        containerScroll: true,
        rootVisible: false,
        frame: true,
        loader:this.loader,
        listeners: {
            checkchange: this.onCheckChildren
        },
        root: {
            id: 0,
			text: '用户选择',
			iconCls: 'icon-group',
			expanded:false
        }
    });
    
    SysMgr.GroupEdit2.superclass.constructor.call(this,{
		title: '群组维护',
		autoHeight: true,
        width: 400,
        y:5,    
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form,this.tree],
		buttons:[{
			text: '保存',
			cls: 'x-btn-text-icon btn-save',
			handler: this.onSave,
			scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
}

Ext.extend(SysMgr.GroupEdit2,Ext.Window,{
	initWin:function(data){
		this.txtName.setValue(data.roleName);
		var root=this.tree.getRootNode();
		this.tree.getRootNode().expand(false);
	},
	onCheckChildren:function(node,checked){
		if(node.isLeaf())return;
		scope=this;
		if(node.expanded){
			Ext.each(node.childNodes, function(child){
	        	child.getUI().toggleCheck(checked);	            
	        });
		}else{
			node.expand(false,false,function(node){
				Ext.each(node.childNodes, function(child){
		        	child.getUI().toggleCheck(checked);	            
		        });
			});
		}
	},
	expandBranch:function(loader,node){		
		if(node.getDepth()>0)
			loader.baseParams.id = node.id;
		//alert(Ext.util.JSON.encode(loader.baseParams));
	},
	onSave:function(){
		if(!this.form.getForm().isValid()){
			Utils.info2("请检查群组名称！",180);
			return;
		}
		var ids='';
		var names='';
		var depts='';
        var selNodes = this.tree.getChecked();
        Ext.each(selNodes, function(node){
        	if(node.isLeaf()){
        		pnode=node.parentNode;
        		depts+=pnode.text+',';
	            ids+=node.id+',';
	            names+=node.text+',';	            
        	}
        });
        scope=this;        
        Ext.Ajax.request({
		   url: '../sysmanager/saveGroup.form?snaptime=' + Date.parse(new Date()),
		   params: {ruserIds:ids,ruserNames:names,rmk:depts,roleId:this.roleId,parentId:this.parentId,roleName:this.txtName.getValue()},
		   success: function(response){
		   	scope.hide();
			Utils.info2();			
			var pwin=scope.parentWin;
			var t=pwin.tree.getRootNode();
			//alert(t.id+'--'+t.text);
			pwin.tree.getLoader().load(pwin.tree.getRootNode());			
		   }
		});    
    }
});